package com.smedi.ismedi.inmail.core.port.adapter.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smedi.ismedi.authoritymanagement.api.dto.response.FrameStructureResponse;
import com.smedi.ismedi.authoritymanagement.api.dto.response.RoleBasicResponse;
import com.smedi.ismedi.authoritymanagement.api.dto.response.WebClientStructureResponse;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAuthorityAccessFeignApi;
import com.smedi.ismedi.authoritymanagement.api.feign.RemoteAuthorityRoleManagementFeignApi;
import com.smedi.ismedi.inmail.core.domain.aggregatemodel.announcement.CurrentUserRole;
import com.smedi.ismedi.inmail.core.infrastructure.constant.AnnouncementError;
import com.smedi.ismedi.inmail.core.port.adapter.service.AuthorityRoleManagementRemoteApiService;
import com.smedi.ismedi.personnel.api.dto.response.InternalAssignmentResponse;
import com.smedi.ismedi.personnel.api.dto.response.ProjectAssignmentResponse;
import com.smedi.ismedi.personnel.api.feign.RemotePersonCoreFeignApi;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 远程角色接口实现类
 *
 * @author jiangqinxuan
 * @date 2021/07/27
 */
@Service
@AllArgsConstructor
public class AuthorityRoleManagementRemoteApiServiceImpl implements AuthorityRoleManagementRemoteApiService {
    private final RemoteAuthorityRoleManagementFeignApi remoteAuthorityRoleManagementFeignApi;
    private final RemotePersonCoreFeignApi remotePersonCoreFeignApi;

    private final RemoteAuthorityAccessFeignApi remoteAuthorityAccessFeignApi;
    private final ObjectMapper objectMapper;


    @Override
    public CurrentUserRole getCurrentUserRole(Long currentUserId) {
        CurrentUserRole currentUserRole = new CurrentUserRole();
        List<InternalAssignmentResponse> internalAssignmentResponseList;
        List<ProjectAssignmentResponse> projectAssignmentResponseList;

        List<RoleBasicResponse> roleBasicResponseList = remoteAuthorityRoleManagementFeignApi.getQueryRolesByPersonId(currentUserId).getData();
        assert roleBasicResponseList != null : "当前登录人角色为空";
        for (RoleBasicResponse roleBasicResponseTemp : roleBasicResponseList) {
            assert roleBasicResponseTemp.getRoleName() != null : "当前登录人角色名称为空";
            if ("主体院系统管理员".equals(roleBasicResponseTemp.getRoleName())) {
                currentUserRole.setOrganizationResponsible(true);
                // 找到所属的主体院
                internalAssignmentResponseList = remotePersonCoreFeignApi.getQueryPersonDetailInfo(currentUserId).getData().getInternalAssignments();
                // 如果登录人没有所属主体院
                if (internalAssignmentResponseList == null) {
                    throw LogicException.raise(AnnouncementError.RESPONSIBLE_ORGANIZATION_MISS);
                }
                // 登录人的所属主体院，就认为他是该主体院的负责人
                List<String> hrOrganizationNameList = new ArrayList<>();
                for (InternalAssignmentResponse internalAssignmentResponseTemp : internalAssignmentResponseList) {
                    hrOrganizationNameList.add(internalAssignmentResponseTemp.getHrOrganizationName());
                }
                currentUserRole.setOrganizationNameList(hrOrganizationNameList);
            }
            if ("EPC项目经理".equals(roleBasicResponseTemp.getRoleName())) {
                currentUserRole.setProjectResponsible(true);
                // 找到负责的项目
                projectAssignmentResponseList = remotePersonCoreFeignApi.getQueryPersonDetailInfo(currentUserId).getData().getProjectAssignments();
                // 如果登录人没有所属项目
                if (projectAssignmentResponseList == null) {
                    throw LogicException.raise(AnnouncementError.RESPONSIBLE_PROJECT_MISS);
                }
                // 登录人的所属主体院，就认为他是该主体院的负责人
                List<String> projectNameList = new ArrayList<>();
                for (ProjectAssignmentResponse projectAssignmentResponseTemp : projectAssignmentResponseList) {
                    projectNameList.add(projectAssignmentResponseTemp.getProjectName());
                }
                currentUserRole.setProjectNameList(projectNameList);
            }
        }

        return currentUserRole;
    }

    @Override
    public Boolean getAccessToIncrease() {
        WebClientStructureResponse pcFrame = remoteAuthorityAccessFeignApi.getMenuInfrastructure("PC_Frame").getData();
        List<FrameStructureResponse> structureInfos = pcFrame.getStructureInfos();
        for (FrameStructureResponse frameStructureResponse : structureInfos) {
            if (frameStructureResponse.getType().equals("TermMenu")) {
                if (ObjectUtils.isNull(frameStructureResponse.getTerm())) {
                    continue;
                }
                LinkedHashMap term = (LinkedHashMap) frameStructureResponse.getTerm();
                if (term.get("title").equals("通知公告")) {
                    for (Object frameStructureResponseChildObject : frameStructureResponse.getChildren()) {
                        FrameStructureResponse frameStructureResponseChild = (FrameStructureResponse) frameStructureResponseChildObject;
                        if (ObjectUtils.isNull(frameStructureResponseChild.getTerm())) {
                            continue;
                        }
                        LinkedHashMap childTerm = (LinkedHashMap) frameStructureResponseChild.getTerm();
                        if (childTerm.get("title").equals("通知公告管理")) {
                            return true;
                        }
                    }
                }
            }
        }

        return false;
    }
}
